<?php
require_once("vcl/vcl.inc.php");
require_once("lib/db_connection.php");
require_once("Writer.php");
//Includes
use_unit("platinumgrid/jtplatinumgrid.inc.php");
use_unit("components4phpfull/jtsitetheme.inc.php");
use_unit("forms.inc.php");
use_unit("extctrls.inc.php");
use_unit("stdctrls.inc.php");

//Class definition
class Veh_Reports extends Page
{
       public $hiddentype = null;
       public $hiddenReportDate = null;
       public $JTGridReports = null;
       public $hiddenVeh = null;
       public $JTSiteTheme1 = null;

       function Veh_ReportsCreate($sender, $params)
       {
       $veh = $_GET['veh'];
       $report = $_GET['report_date'];
       $type = $_GET['type'];
       $this->hiddenReportDate->setValue($report);
       $this->hiddenVeh->setValue($veh);
       $this->hiddentype->setValue($type);
       $veh_id = $this->hiddenVeh->getValue();
       $report_date = $this->hiddenReportDate->getValue();
       $report_type = $this->hiddentype->getValue();


       $query_veh = "select * from vehicle_tbl where vehicle_id='".$veh_id."'";
       $result_veh = mysql_query($query_veh)or die(mysql_error());
       $row_veh = mysql_fetch_array($result_veh);

       $arr = array();

       $arr[] = array(
                        '1' => 'Vehicle Num:'.' '.$row_veh['VIN'],
                        '2'=>' ',
                        '3' => ' ',
                        '4' => ' ',
                        '5' => ' ',
                        '6' => ' ',
                        '7' => ' ',
                        '8' => ' ',
                        '9' => ' ',
                        '10' => ' ',
                        '11' => ' '
                    );


       $arr[] = array(
                        '1' => 'Plate Num:'.' '.$row_veh['veh_num'],
                        '2'=> ' ',
                        '3' => ' ',
                        '4' => ' ',
                        '5' => ' ',
                        '6' => ' ',
                        '7' => ' ',
                        '8' => ' ',
                        '9' => ' ',
                        '10' => ' ',
                        '11' => ' '
                    );

       $arr[] = array(
                        '1' => 'Year:'.' '.$row_veh['year'],
                        '2'=>' ',
                        '3' => ' ',
                        '4' => ' ',
                        '5' => ' ',
                        '6' => ' ',
                        '7' => ' ',
                        '8' => ' ',
                        '9' => ' ',
                        '10' => ' ',
                        '11' => ' '
                    );

       $arr[] = array(
                        '1' => 'Make:'.' '.$row_veh['manufacturer_name'],
                        '2'=>' ',
                        '3' => ' ',
                        '4' => ' ',
                        '5' => ' ',
                        '6' => ' ',
                        '7' => ' ',
                        '8' => ' ',
                        '9' => ' ',
                        '10' => ' ',
                        '11' => ' '
                    );

       $arr[] = array(
                        '1' => 'Model:'.' '.$row_veh['model_name'],
                        '2'=> ' ',
                        '3' => ' ',
                        '4' => ' ',
                        '5' => ' ',
                        '6' => ' ',
                        '7' => ' ',
                        '8' => ' ',
                        '9' => ' ',
                        '10' => ' ',
                        '11' => ' '
                    );

       $arr[] = array(
                        '1' => 'Type/Style:'.' '.$row_veh['style_name'],
                        '2'=> ' ',
                        '3' => ' ',
                        '4' => ' ',
                        '5' => ' ',
                        '6' => ' ',
                        '7' => ' ',
                        '8' => ' ',
                        '9' => ' ',
                        '10' => ' ',
                        '11' => ' '
                    );

       $arr[] = array(
                        '1' => ' ',
                        '2'=>' ',
                        '3' => ' ',
                        '4' => ' ',
                        '5' => ' ',
                        '6' => ' ',
                        '7' => ' ',
                        '8' => ' ',
                        '9' => ' ',
                        '10' => ' ',
                        '11' => ' '
                    );



       $query1 = "select * from veh_fuel_tbl where vehicle_id='".$veh_id."' and date like '".$report_date."'";
       $result = mysql_query($query1)or die(mysql_error());
       $label_od = $row['track_by'];


       if ($label_od=='distance')
       {
       $string1 = 'Prev Odometer';
       $string2 = 'Curr Odometer';
       }
       else
       {
       $string1 = 'Last Clock Hrs';
       $string2 = 'Clock Hrs Now';
       }

      $arr[] = array(
                        '1' => 'FUEL REPORT',
                        '2'=>' ',
                        '3' => ' ',
                        '4' => ' ',
                        '5' => ' ',
                        '6' => ' ',
                        '7' => ' ',
                        '8' => ' ',
                        '9' => ' ',
                        '10' => ' ',
                        '11' => ' '
                    );

      $arr[] = array(
      '1' => 'Date',
      '2'=> 'Driver',
      '3' => 'Brand',
      '4' => 'Engine',
      '5' => 'Account',
      '6' => $string1,
      '7' => $string2,
      '8' => 'Price/Li',
      '9' => 'Liters',
      '10' => 'Tot Cost',
      '11' => 'Mileage'
      );
      while($row = mysql_fetch_array($result))
      {

         $arr[] = array(
                        '1' => $row['date'],
                        '2'=>$row['driver'],
                        '3' => $row['brand'],
                        '4' => $row['engine'],
                        '5' => $row['account'],
                        '6' => $row['last_odometer'],
                        '7' => $row['present_odometer'],
                        '8' => $row['price'],
                        '9' => $row['gallons'],
                        '10' => $row['tot_cost'],
                        '11' => $row['miles']

                        );

      }
      $total_fuel = mysql_query("SELECT SUM(miles) as sum_miles,
      SUM(gallons) as sum_gallons,
      SUM(tot_cost) as sum_cost
      FROM veh_fuel_tbl where vehicle_id='".$veh_id."' and date like '".$report_date."'");
      $row_fuel = mysql_fetch_array($total_fuel);

      $arr[] = array(
                        '1' => TOTAL,
                        '11' => $row_fuel['sum_miles'],
                        '9' => $row_fuel['sum_gallons'],
                        '10' => $row_fuel['sum_cost'],
                        );

      $arr[] = array(
                        '1' => ' ',
                        '2'=>' ',
                        '3' => ' ',
                        '4' => ' ',
                        '5' => ' ',
                        '6' => ' ',
                        '7' => ' ',
                        '8' => ' ',
                        '9' => ' ',
                        '10' => ' '
                        );
      $arr[] = array(
                        '1' => 'WORK SUMMARY - LABORLINE REPORT'
                    );
      $arr[] = array(
                        '1' => 'Description',
                        '2'=> 'Units',
                        '3' => 'Cost/Unit',
                        '4' => 'Total'
                        );

       $query1 = "select * from veh_laborline_tbl where vehicle_id='".$this->hiddenVeh->getValue()."' and labor_date like '".$report_date."'";
       $result = mysql_query($query1)or die(mysql_error());

       while($row = mysql_fetch_array($result))
       {

       $labor_unit = $row['description'];
       $arr[] = array(
                        '1' => $labor_unit,
                        '2' => $row['labor_unit'],
                        '3' => $row['cost'],
                        '4' => $row['total']
                        );
       }

      $total_amount = mysql_query("SELECT SUM(total) as sum_amount FROM veh_laborline_tbl where vehicle_id='".$this->hiddenVeh->getValue()."' and labor_date like '".$report_date."'");
      $row_amount = mysql_fetch_array($total_amount);


      $arr[] = array(
                        '1' => TOTAL,
                        '4' => $row_amount['sum_amount']
                        );

      $arr[] = array(
                        '1' => ' ',
                        '2'=>' ',
                        '3' => ' ',
                        '4' => ' ',
                        '5' => ' ',
                        '6' => ' ',
                        '7' => ' ',
                        '8' => ' ',
                        '9' => ' ',
                        '10' => ' '
                        );

       $arr[] = array(
                        '1' => 'WORK SUMMARY - PARTS REPORT'
                    );
       $arr[] = array(
                        '1' => 'Stock/Part No',
                        '2'=> 'Qty',
                        '3' => 'Unit Price',
                        '4' => 'Amount'
                        );

       $query_parts = "select * from veh_parts_tbl where vehicle_id='".$veh_id."' and parts_date like '".$report_date."'";
       $result_parts = mysql_query($query_parts)or die(mysql_error());

       while($row_parts = mysql_fetch_array($result_parts))
       {

       $arr[] = array(
                        '1' => $row_parts['part_num'],
                        '2' => $row_parts['quantity'],
                        '3' => $row_parts['price_ea'],
                        '4' => $row_parts['amount']
                        );
       }

       $total_parts = mysql_query("SELECT SUM(amount) as sum_amount FROM veh_parts_tbl where vehicle_id='".$this->hiddenVeh->getValue()."' and parts_date like '".$report_date."'");
       $row_amt_parts = mysql_fetch_array($total_parts);

        $arr[] = array(
                        '1' => TOTAL,
                        '4' => $row_amt_parts['sum_amount']
                        );


       $arr[] = array(
                        '1' => ' ',
                        '2'=>' ',
                        '3' => ' ',
                        '4' => ' ',
                        '5' => ' ',
                        '6' => ' ',
                        '7' => ' ',
                        '8' => ' ',
                        '9' => ' ',
                        '10' => ' '
                        );

       $arr[] = array(
                        '1' => 'EXPENSE REPORT'
                    );


       $arr[] = array(
                        '1' => 'Date',
                        '2'=> 'Description',
                        '3' => 'Type',
                        '4' => 'Amount'
                        );



       $query1 = "select * from veh_expense_tbl where vehicle_id='".$veh_id."' and date like '".$report_date."'";
       $result = mysql_query($query1)or die(mysql_error());

       while($row = mysql_fetch_array($result))
       {

       $arr[] = array(
                        '1' => $row['date'],
                        '2' => $row['description'],
                        '3' => $row['type'],
                        '4' => $row['amount']
                        );
       }

      $total_amount = mysql_query("SELECT SUM(amount) as sum_amount FROM veh_expense_tbl where vehicle_id='".$this->hiddenVeh->getValue()."' and date like '".$report_date."'");
      $row_amount = mysql_fetch_array($total_amount);


      $arr[] = array(
                        '1' => TOTAL,
                        '4' => $row_amount['sum_amount']
                        );



      //Emission
      $arr[] = array(
                        '1' => ' ',
                        '2'=>' ',
                        '3' => ' ',
                        '4' => ' ',
                        '5' => ' ',
                        '6' => ' ',
                        '7' => ' ',
                        '8' => ' ',
                        '9' => ' ',
                        '10' => ' '
                        );

       $arr[] = array(
                        '1' => 'EMISSION EXPENSE REPORT'
                    );


       $arr[] = array(
                        '1' => 'Test Date',
                        '2'=> 'Valid Thru',
                        '3' => 'Inspector',
                        '4' => 'Account',
                        '5' => 'Expense'
                        );


       $query_emission = "select * from veh_emission_tbl where vehicle_id='".$veh_id."' and inspection_date like '".$report_date."'";
       $result_emission = mysql_query($query_emission)or die(mysql_error());

       while($row_emission = mysql_fetch_array($result_emission))
       {

       $arr[] = array(
                        '1' => $row_emission['inspection_date'],
                        '2' => $row_emission['validity_date'],
                        '3' => $row_emission['inspector'],
                        '4' => $row_emission['account'],
                        '5' => $row_emission['test_fee']
                        );
       }

      $total_emission = mysql_query("SELECT SUM(test_fee) as sum_amt FROM veh_emission_tbl where vehicle_id='".$this->hiddenVeh->getValue()."' and inspection_date like '".$report_date."'");
      $row_tot = mysql_fetch_array($total_emission);


      $arr[] = array(
                        '1' => TOTAL,
                        '5' => $row_tot['sum_amt']
                        );


       $this->JTGridReports->setCellData($arr);
       $this->JTGridReports->exportGridToXLSDownload('Vehicle_Report.xls');
       }



       function JTGridReportsShow($sender, $params)
       {
       $veh = $_GET['veh'];
       $this->hiddenVeh->setValue($veh);
       $veh_id = $this->hiddenVeh->getValue();


       $query_veh = "select * from vehicle_tbl where vehicle_id='".$veh_id."'";
       $result_veh = mysql_query($query_veh)or die(mysql_error());


       $arr = array();

       $arr[] = array(
                        '1' => 'VIN',
                        '2'=>' ',
                        '3' => ' ',
                        '4' => ' ',
                        '5' => ' ',
                        '6' => ' ',
                        '7' => ' ',
                        '8' => ' ',
                        '9' => ' ',
                        '10' => ' '
                    );

      while($row_veh = mysql_fetch_array($result_veh))
      {

         $arr[] = array(
                        '1' => $row_veh['VIN']
                        );

      }




       $query1 = "select * from veh_fuel_tbl where vehicle_id='".$veh_id."'";
       $result = mysql_query($query1)or die(mysql_error());
       $label_od = $row['track_by'];


       if ($label_od=='distance')
       {
       $string1 = 'Prev Odometer';
       $string2 = 'Curr Odometer';
       }
       else
       {
       $string1 = 'Last Clock Hrs';
       $string2 = 'Clock Hrs Now';
       }

      $arr = array();

      $arr[] = array(
                        '1' => 'FUEL REPORT',
                        '2'=>' ',
                        '3' => ' ',
                        '4' => ' ',
                        '5' => ' ',
                        '6' => ' ',
                        '7' => ' ',
                        '8' => ' ',
                        '9' => ' ',
                        '10' => ' '
                    );

      $arr[] = array(
      '1' => 'Date',
      '2'=> 'Driver',
      '3' => 'Brand',
      '4' => 'Engine',
      '5' => $string1,
      '6' => $string2,
      '7' => 'Price/Li',
      '8' => 'Liters',
      '9' => 'Tot Cost',
      '10' => 'Mileage'
      );
      while($row = mysql_fetch_array($result))
      {

         $date = "<a href=veh_fuel_edit.php?veh=".$veh_id."&fuel=".$row['fuel_id']." title='Edit Fuel Record'>".$row['date']."</a>";
         $tools = "<img onclick=js_deleteFuel('".$veh_id."','".$row['fuel_id']."'); src='image/delete_orange.jpg' title='delete'>";
         //"<a href=veh_fuel_delete.php?veh=".$veh_id."&fuel=".$row['fuel_id']."><img src='image/delete_orange.jpg' title='delete' no border></a>";
         $driver = "<a href='#' onClick=showDriInfo('".$empid."','".urlencode($lname)."','".urlencode($fname)."','".urlencode($mname)."','".$email."','".$phone."','".$mobile."','".urlencode($Dept)."') title='Driver Information'>".$row['driver']."</a>";
         //"<img onclick=showDriInfo('".$empid."','".urlencode($lname)."','".urlencode($fname)."','".urlencode($mname)."','".$email."','".$phone."','".$mobile."','".urlencode($Dept)."'); src='image/orange_information.jpg' title='Driver Information'>";

         $arr[] = array(
                        '1' => $row['date'],
                        '2'=>$row['driver'],
                        '3' => $row['brand'],
                        '4' => $row['engine'],
                        '5' => $row['last_odometer'],
                        '6' => $row['present_odometer'],
                        '7' => $row['price'],
                        '8' => $row['gallons'],
                        '9' => $row['tot_cost'],
                        '10' => $row['miles']
                        );

      }
      $total_fuel = mysql_query("SELECT SUM(miles) as sum_miles,
      SUM(gallons) as sum_gallons,
      SUM(tot_cost) as sum_cost
      FROM veh_fuel_tbl where vehicle_id='".$veh_id."'");
      $row_fuel = mysql_fetch_array($total_fuel);

      $arr[] = array(
                        '1' => TOTAL,
                        '10' => $row_fuel['sum_miles'],
                        '8' => $row_fuel['sum_gallons'],
                        '9' => $row_fuel['sum_cost'],
                        );

      $arr[] = array(
                        '1' => ' ',
                        '2'=>' ',
                        '3' => ' ',
                        '4' => ' ',
                        '5' => ' ',
                        '6' => ' ',
                        '7' => ' ',
                        '8' => ' ',
                        '9' => ' ',
                        '10' => ' '
                        );
      $arr[] = array(
                        '1' => 'WORKORDER - LABORLINE REPORT'
                    );
      $arr[] = array(
                        '1' => 'Description',
                        '2'=> 'Units',
                        '3' => 'Cost/Unit',
                        '4' => 'Total'
                        );

       $query1 = "select * from veh_laborline_tbl where vehicle_id='".$this->hiddenVeh->getValue()."'";
       $result = mysql_query($query1)or die(mysql_error());

       while($row = mysql_fetch_array($result))
       {

       $labor_unit = $row['description'];
       $arr[] = array(
                        '1' => $labor_unit,
                        '2' => $row['labor_unit'],
                        '3' => $row['cost'],
                        '4' => $row['total']
                        );
       }

      $total_amount = mysql_query("SELECT SUM(total) as sum_amount FROM veh_laborline_tbl where vehicle_id='".$this->hiddenVeh->getValue()."'");
      $row_amount = mysql_fetch_array($total_amount);


      $arr[] = array(
                        '1' => TOTAL,
                        '4' => $row_amount['sum_amount']
                        );

      $arr[] = array(
                        '1' => ' ',
                        '2'=>' ',
                        '3' => ' ',
                        '4' => ' ',
                        '5' => ' ',
                        '6' => ' ',
                        '7' => ' ',
                        '8' => ' ',
                        '9' => ' ',
                        '10' => ' '
                        );

       $arr[] = array(
                        '1' => 'WORKORDER - PARTS REPORT'
                    );
       $arr[] = array(
                        '1' => 'Stock/Part No',
                        '2'=> 'Qty',
                        '3' => 'Unit Price',
                        '4' => 'Amount'
                        );

       $query_parts = "select * from veh_parts_tbl where vehicle_id='".$veh_id."'";
       $result_parts = mysql_query($query_parts)or die(mysql_error());

       while($row_parts = mysql_fetch_array($result_parts))
       {

       $arr[] = array(
                        '1' => $row_parts['part_num'],
                        '2' => $row_parts['quantity'],
                        '3' => $row_parts['price_ea'],
                        '4' => $row_parts['amount']
                        );
       }

       $total_parts = mysql_query("SELECT SUM(amount) as sum_amount FROM veh_parts_tbl where vehicle_id='".$this->hiddenVeh->getValue()."'");
       $row_amt_parts = mysql_fetch_array($total_parts);

        $arr[] = array(
                        '1' => TOTAL,
                        '4' => $row_amt_parts['sum_amount']
                        );


       $arr[] = array(
                        '1' => ' ',
                        '2'=>' ',
                        '3' => ' ',
                        '4' => ' ',
                        '5' => ' ',
                        '6' => ' ',
                        '7' => ' ',
                        '8' => ' ',
                        '9' => ' ',
                        '10' => ' '
                        );

       $arr[] = array(
                        '1' => 'EXPENSE REPORT'
                    );


       $arr[] = array(
                        '1' => 'Date',
                        '2'=> 'Description',
                        '3' => 'Type',
                        '4' => 'Amount'
                        );



       $query1 = "select * from veh_expense_tbl where vehicle_id='".$veh_id."'";
       $result = mysql_query($query1)or die(mysql_error());

       while($row = mysql_fetch_array($result))
       {

       $arr[] = array(
                        '1' => $row['date'],
                        '2' => $row['description'],
                        '3' => $row['type'],
                        '4' => $row['amount']
                        );
       }

      $total_amount = mysql_query("SELECT SUM(amount) as sum_amount FROM veh_expense_tbl where vehicle_id='".$this->hiddenVeh->getValue()."'");
      $row_amount = mysql_fetch_array($total_amount);


      $arr[] = array(
                        '1' => TOTAL,
                        '4' => $row_amount['sum_amount']
                        );




       $this->JTGridReports->setCellData($arr);
       $this->JTGridReports->exportGridToXLSDownload('Vehicle_Report.xls');
       }

}

global $application;

global $Veh_Reports;

//Creates the form
$Veh_Reports=new Veh_Reports($application);

//Read from resource file
$Veh_Reports->loadResource(__FILE__);

//Shows the form
$Veh_Reports->show();

?>